package org.dromara.agriculture.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.agriculture.domain.vo.AgricultureLandVo;
import org.dromara.agriculture.domain.bo.AgricultureLandBo;
import org.dromara.agriculture.service.IAgricultureLandService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 地块
 *
 * @author Robin
 * @date 2024-12-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/agriculture/land")
public class AgricultureLandController extends BaseController {

    private final IAgricultureLandService agricultureLandService;

    /**
     * 查询地块列表
     */
    @SaCheckPermission("agriculture:land:list")
    @GetMapping("/list")
    public TableDataInfo<AgricultureLandVo> list(AgricultureLandBo bo, PageQuery pageQuery) {
        return agricultureLandService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出地块列表
     */
    @SaCheckPermission("agriculture:land:export")
    @Log(title = "地块", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AgricultureLandBo bo, HttpServletResponse response) {
        List<AgricultureLandVo> list = agricultureLandService.queryList(bo);
        ExcelUtil.exportExcel(list, "地块", AgricultureLandVo.class, response);
    }

    /**
     * 获取地块详细信息
     *
     * @param landId 主键
     */
    @SaCheckPermission("agriculture:land:query")
    @GetMapping("/{landId}")
    public R<AgricultureLandVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long landId) {
        return R.ok(agricultureLandService.queryById(landId));
    }

    /**
     * 新增地块
     */
    @SaCheckPermission("agriculture:land:add")
    @Log(title = "地块", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AgricultureLandBo bo) {
        return toAjax(agricultureLandService.insertByBo(bo));
    }

    /**
     * 修改地块
     */
    @SaCheckPermission("agriculture:land:edit")
    @Log(title = "地块", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AgricultureLandBo bo) {
        return toAjax(agricultureLandService.updateByBo(bo));
    }

    /**
     * 删除地块
     *
     * @param landIds 主键串
     */
    @SaCheckPermission("agriculture:land:remove")
    @Log(title = "地块", businessType = BusinessType.DELETE)
    @DeleteMapping("/{landIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] landIds) {
        return toAjax(agricultureLandService.deleteWithValidByIds(List.of(landIds), true));
    }
}
